package com.bosssoft.hr.train.spring.example.service.impl;


import com.bosssoft.hr.train.spring.example.bean.User;
import com.bosssoft.hr.train.spring.example.dao.impl.UserDaoImp;
import com.bosssoft.hr.train.spring.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description User业务层 为用户类提供数据库的增删改查操作，为了简单的演示bean的注入，这里未作关于业务的异常处理
 * @date 2020/06/04
 * @author ChenTong
 */
@Slf4j
public class UserServiceImpl implements UserService {

    private UserDaoImp userDao;

    //通过constructor方式注入
    public UserServiceImpl(UserDaoImp userDaoImp) {
        this.userDao = userDaoImp;
    }
    public UserServiceImpl() {

    }

    /**
     * 保存用户到数据库
     * @param user
     * @return Boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(User user) {
        log.info("service save");
        return user!=null&&user.getId()!=null&&user.getPassword()!=null
                &&user.getCode()!=null&&user.getName()!=null
                &&-1 != userDao.save(user);

    }

    /**
     * 通过id删除用户
     * @param id : 用户id
     * @return Boolean
     */
    @Transactional
    @Override
    public boolean remove(Long id) {
            return (id!=null)&&(0!=userDao.remove(id));
    }

    /**
     * 更新用户
     * @param user : 需要更新的用户
     * @return Boolean
     */
    @Transactional
    @Override
    public boolean update(User user) {

        return user!=null&&user.getId()!=null&&user.getPassword()!=null
                &&user.getCode()!=null&&user.getName()!=null
                &&-1 != userDao.update(user);
    }

    @Override
    public List<User> query() {
        return userDao.query();
    }

    public UserDaoImp getUserDaoImp() {
        return userDao;
    }

    public void setUserDaoImp(UserDaoImp userDaoImp) {
        this.userDao = userDaoImp;
    }

    public void init(){
        log.info("UserService-bean:init()");
    }

    public void destroy(){
        log.info("UserService-bean:destroy()");
    }
}
